import { BrowserWindow, ipcMain, Menu, app } from "electron";
import { openCalendarWindow } from "@/main/window";

ipcMain.on('test', function (e, data) {
	console.group('接受到通信');
	console.log('data', data)
	console.groupEnd();
});

ipcMain.on('show-context-menu', (event) => {
	const template = [
		{
			label: 'Menu Item 1',
			click: () => {
				// 和渲染进程通讯
				event.sender.send('context-menu-command', 'menu-item-1')
			}
		},
		{type: 'separator'},
		{
			label: 'Menu Item 2',
			type: 'checkbox',
			checked: () => {
				// 主动通知渲染进程
				(<any>BrowserWindow.getFocusedWindow()).webContents.send('xx', 'new 111');
			}
		}
	]
	const menu = Menu.buildFromTemplate(<[]>template);
	menu.popup(<any>BrowserWindow.fromWebContents(event.sender))
})

ipcMain.on('openCalendarWindow', e =>
	openCalendarWindow()
);

ipcMain.on('getPath', event => {
	const userDataPath = app.getPath('userData');
	const appPath = app.getAppPath();

	event.sender.send('path', {
		appPath,
		userDataPath
	})
});

ipcMain.on('store', (event, data) => {
	console.log('data', data)
});

ipcMain.on('getAllRender', (event, data) => {
	// const winId = (BrowserWindow as any).getFocusedWindow().id;
	console.group('getAllRender');
	// todo 获取所有渲染进程
	const arr = BrowserWindow.getAllWindows();
	const win = BrowserWindow.fromId(1);
	// console.log('winId', winId)
	console.log('arr', arr)
	console.groupEnd();
	const a = { b: 1};

	const val = a?.b; // 报错

	console.log(val);

	// 循环BrowserWindow ID数组
	(win as any).webContents.send('getAllRender', '112313')
});
